package com.pur.zsms.quotation.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.PubFun;
import com.pur.zsms.company.dao.ICompanyDao;
import com.pur.zsms.company.po.CompanyPo;
import com.pur.zsms.company.vo.CompanyVo;
import com.pur.zsms.matermanage.mater.dao.IMaterDao;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.quotation.dao.IQuotationDao;
import com.pur.zsms.quotation.po.QuotationCompanyPeriodPo;
import com.pur.zsms.quotation.po.QuotationCompanyPeriodSubPo;
import com.pur.zsms.quotation.po.QuotationPo;
import com.pur.zsms.quotation.po.QuotationSubPo;
import com.pur.zsms.quotation.service.IQuotationService;
import com.pur.zsms.quotation.vo.QuotationCompanyPeriodSubVo;
import com.pur.zsms.quotation.vo.QuotationCompanyPeriodVo;
import com.pur.zsms.quotation.vo.QuotationVo;
import com.pur.zsms.shopmanage.dao.IShopDao;

@Service("quotationService")
public class QuotationServiceImpl implements IQuotationService {
	String msg = "";
	String content = "";

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	@Resource(name = "quotationDao")
	private IQuotationDao quotationDao;
	@Resource(name = "materDao")
	private IMaterDao materDaoImpl;

	@Resource(name = "shopDao")
	private IShopDao shopDaoImpl;

	@Resource(name = "companyDao")
	private ICompanyDao companyDao;

	public boolean checkSaveQuotation(String quotationName, long supId) throws Exception {

		// 检查是否有同名
		boolean checkflag = quotationDao.checkName(quotationName, supId);
		if (!checkflag) {
			msg = "001";
			return false;
		}
		return true;
	}

	public QuotationPo saveQuotation(String quotationName, long supId) throws Exception {
		QuotationPo quotationPo = new QuotationPo();
		quotationPo.setQuotationName(quotationName);
		quotationPo.setSupId(supId);
		quotationDao.addQuotation(quotationPo);
		return quotationPo;
	}

	public void saveQuotationSub(QuotationSubPo quotationSubPo) throws Exception {
		quotationDao.saveQuotationSub(quotationSubPo);
	}

	public boolean addQuotationMaterByShopIdMaterIdForSup(QuotationCompanyPeriodSubVo c) throws Exception {
		// 先查询公司有没有报价
		int companyId = (int) shopDaoImpl.getShopPoByShopId(c.getShopId()).get(0).getCompanyUser();

		QuotationCompanyPeriodVo tQuotationCompanyPeriodVo = new QuotationCompanyPeriodVo();
		tQuotationCompanyPeriodVo.setSupId(c.getSupId());
		tQuotationCompanyPeriodVo.setCompanyId(companyId);
		tQuotationCompanyPeriodVo.setQuotationFlag(1);
		List<QuotationCompanyPeriodPo> listQuotationCompanyPeriodPo = quotationDao.getQuotationCompanyPeriodPo(tQuotationCompanyPeriodVo);
		if (listQuotationCompanyPeriodPo != null && listQuotationCompanyPeriodPo.size() > 0) {
			// 查询是否有材料
			QuotationCompanyPeriodSubVo tQuotationCompanyPeriodSubVo = new QuotationCompanyPeriodSubVo();
			tQuotationCompanyPeriodSubVo.setQuotationId(listQuotationCompanyPeriodPo.get(0).getQuotationId());
			tQuotationCompanyPeriodSubVo.setMaterId(c.getMaterId());
			List<QuotationCompanyPeriodSubPo> listQuotationCompanyPeriodSubPo = quotationDao
					.getQuotationCompanyPeriodSubPo(tQuotationCompanyPeriodSubVo);
			if (listQuotationCompanyPeriodSubPo != null && listQuotationCompanyPeriodSubPo.size() > 0) {
				QuotationCompanyPeriodSubPo po = listQuotationCompanyPeriodSubPo.get(0);
				po.setMaterValue(c.getMaterValue());
				quotationDao.updateQuotationCompanyPeriodSubPo(po);
			} else {
				QuotationCompanyPeriodSubPo po = new QuotationCompanyPeriodSubPo();
				po.setQuotationId(listQuotationCompanyPeriodPo.get(0).getQuotationId());
				po.setMaterId(c.getMaterId());
				po.setMaterValue(c.getMaterValue());
				quotationDao.updateQuotationCompanyPeriodSubPo(po);
			}
		} else {
			msg = "该公司没有周期报价！";
			return false;
		}
		return true;
	}

	public void deleteQuotation(long quotationId) throws Exception {
		quotationDao.deleteQuotation(quotationId);
	}

	public PaginationSupport listQuotation(PageController pageController, QuotationVo quotationVo) throws GenericException {
		return quotationDao.listQuotation(pageController, quotationVo);
	}

	public String pullDownQuotation(String codeId, String codeName, long supId) throws GenericException {
		List<QuotationPo> codeList = quotationDao.findQuotationPo(supId);
		StringBuffer jsonMaterTypeString = new StringBuffer("");

		if (codeList != null && codeList.size() > 0) {

			jsonMaterTypeString.append("[");
			for (QuotationPo code : codeList) {
				jsonMaterTypeString.append("{\"");
				jsonMaterTypeString.append(codeId);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getQuotationId());
				jsonMaterTypeString.append("\",\"");
				jsonMaterTypeString.append(codeName);
				jsonMaterTypeString.append("\":\"");
				jsonMaterTypeString.append(code.getQuotationName());
				jsonMaterTypeString.append("\"},");
			}
			jsonMaterTypeString = new StringBuffer(jsonMaterTypeString.substring(0, jsonMaterTypeString.length() - 1));
			jsonMaterTypeString.append("]");
		}
		return jsonMaterTypeString.toString();
	}

	public PaginationSupport companyPeriodList(PageController pageController, QuotationCompanyPeriodVo tQuotationCompanyPeriodVo)
			throws GenericException {
		return quotationDao.companyPeriodList(pageController, tQuotationCompanyPeriodVo);

	}

	public PaginationSupport detailRaisePricePeriod(PageController pageController, QuotationCompanyPeriodVo tQuotationCompanyPeriodVo)
			throws GenericException {
		return quotationDao.detailRaisePricePeriod(pageController, tQuotationCompanyPeriodVo);

	}

	public boolean checkImportDetailCompanyPeriod(Map<Integer, String> map) throws Exception {
		// 校验
		if (map.size() < 1) {
			msg = "msg002";
			return false;
		}
		for (int i = 1; i <= map.size(); i++) {
			// 长度校验
			String[] everyline = map.get(i).split("\\^");
			if (everyline == null || everyline.length == 0) {
				break;
			}
			String materId = everyline[0];
			String materValue = everyline[6];

			// 整数校验
			List<MaterPo> materList = materDaoImpl.getMaterPoByMaterId(new BigDecimal(materId).intValue());
			if (materList == null || materList.size() == 0) {
				msg = "msg003";
				content = i + "";
				return false;
			}
			// 验证价格
			String eLvalue = "^[0-9]+(.[0-9]{0,2})?$";
			Pattern p = Pattern.compile(eLvalue);
			Matcher m = p.matcher(materValue);
			boolean valueFlag = m.matches();
			if (!valueFlag) {
				msg = "msg004";
				content = i + "";
				return false;
			}

		}
		return true;
	}

	public void importDetailCompanyPeriod(Map<Integer, String> map, QuotationCompanyPeriodPo tQuotationCompanyPeriodPo) throws Exception {
		for (int i = 1; i <= map.size(); i++) {
			// 长度校验
			String[] everyline = map.get(i).split("\\^");
			if (everyline == null || everyline.length == 0) {
				break;
			}
			String materId = everyline[0];
			String materValue = everyline[6];
			QuotationCompanyPeriodSubPo tQuotationCompanyPeriodSubPo = new QuotationCompanyPeriodSubPo();
			tQuotationCompanyPeriodSubPo.setMaterId(new BigDecimal(materId).intValue());
			tQuotationCompanyPeriodSubPo.setMaterValue(materValue);
			tQuotationCompanyPeriodSubPo.setQuotationId(tQuotationCompanyPeriodPo.getQuotationId());
			quotationDao.addQuotationCompanyPeriodSubPo(tQuotationCompanyPeriodSubPo);
		}

	}

	public QuotationCompanyPeriodPo addQuotationCompanyPeriodPo(long supId, int companyId, String quotationName) throws Exception {
		QuotationCompanyPeriodPo tQuotationCompanyPeriodPo = new QuotationCompanyPeriodPo();
		tQuotationCompanyPeriodPo.setCompanyId(companyId);
		tQuotationCompanyPeriodPo.setQuotationDate(PubFun.getCurrentDate());
		tQuotationCompanyPeriodPo.setQuotationFlag(0);
		tQuotationCompanyPeriodPo.setQuotationName(quotationName);
		tQuotationCompanyPeriodPo.setSupId(supId);
		quotationDao.addQuotationCompanyPeriodPo(tQuotationCompanyPeriodPo);
		return tQuotationCompanyPeriodPo;
	}

	public String getAllCompanyJson(String diy) throws GenericException {
		List<CompanyPo> companyList = quotationDao.getCompanyPo();
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (companyList != null && companyList.size() > 0) {

			jsonUserTestValue.append("[");

			if ("1".equals(diy)) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(0);
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append("无");
				jsonUserTestValue.append("\"},");
			}
			for (CompanyPo companyPo : companyList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(companyPo.getCompanyId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(companyPo.getCompanyName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public String getAllCompanyJsonForSup(String diy, long supId) throws GenericException {
		List<CompanyPo> companyList = quotationDao.getCompanyPoBySupId(supId);
		StringBuffer jsonUserTestValue = new StringBuffer("");

		if (companyList != null && companyList.size() > 0) {

			jsonUserTestValue.append("[");

			if ("1".equals(diy)) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(0);
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append("无");
				jsonUserTestValue.append("\"},");
			}
			for (CompanyPo companyPo : companyList) {
				jsonUserTestValue.append("{\"");
				jsonUserTestValue.append("value");
				jsonUserTestValue.append("\":\"");
				jsonUserTestValue.append(companyPo.getCompanyId());
				jsonUserTestValue.append("\",\"text\":\"");
				jsonUserTestValue.append(companyPo.getCompanyName());
				jsonUserTestValue.append("\"},");
			}
			jsonUserTestValue = new StringBuffer(jsonUserTestValue.substring(0, jsonUserTestValue.length() - 1));
			jsonUserTestValue.append("]");
		}
		return jsonUserTestValue.toString();
	}

	public boolean adoptCompanyPeriod(int supId, long quotationId, int companyId) throws GenericException {

		List<QuotationCompanyPeriodPo> quotationCompanyPeriodPoList = quotationDao.getQuotationCompanyPeriodPoBySupIdAndCompanyId(supId, companyId);

		for (QuotationCompanyPeriodPo quotationCompanyPeriodPo : quotationCompanyPeriodPoList) {
			if (quotationCompanyPeriodPo.getQuotationId() == quotationId) {
				quotationCompanyPeriodPo.setQuotationFlag(1);
				quotationDao.updateQuotationCompanyPeriodPo(quotationCompanyPeriodPo);
			} else if (quotationCompanyPeriodPo.getQuotationId() != quotationId && quotationCompanyPeriodPo.getQuotationFlag() == 1) {
				quotationCompanyPeriodPo.setQuotationFlag(0);
				quotationDao.updateQuotationCompanyPeriodPo(quotationCompanyPeriodPo);
			}
		}
		return true;
	}

	public QuotationCompanyPeriodPo getQuotationCompanyPeriodPo(long quotationId) throws GenericException {
		return quotationDao.getQuotationCompanyPeriodPoByQuotationId(quotationId).get(0);
	}

	public void deleteCompanyPeriod(long quotationId) throws GenericException {
		quotationDao.deleteCompanyPeriod(quotationId);
	}

	public List<Object[]> getCompanyPeriodForExcel(long quotationId) throws GenericException {
		return quotationDao.getCompanyPeriodForExcel(quotationId);

	}

	public String getCompanyPeriodValue(int materId, int supId, long shopId) throws GenericException {

		List<Object[]> materValue = quotationDao.getCompanyPeriodValue(materId, supId, shopDaoImpl.getShopPoByShopId(shopId).get(0).getCompanyUser());
		if (materValue != null && materValue.size() > 0) {
			Object value = materValue.get(0);

			return value.toString();
		} else {
			return "0";
		}
	}

}
